package com.bailiban.aigou.admin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.admin.dao.ProductDao;
import com.bailiban.aigou.admin.entity.BackstageUser;
import com.bailiban.aigou.admin.entity.Classification;
import com.bailiban.aigou.admin.entity.Product;
import com.bailiban.aigou.admin.utils.PageModel;
@Repository
public class ProductDaoImpl implements ProductDao {
	@Resource
	private SessionFactory sessionFactory;
	
	/**
	 * 查询分页数据
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	@Override
	public PageModel<Product> getProductPages(int pageNo, int pageSize) {
		
		
		Session session = sessionFactory.getCurrentSession();
		
		PageModel<Product> pageModel=new PageModel<>();
		pageModel.setPageNo(pageNo);
		pageModel.setPageSize(pageSize);
		
		Criteria criteria = session.createCriteria(Product.class);
		criteria.setFirstResult((pageNo-1)*pageSize);
		criteria.setMaxResults(pageSize);
		
		List<Product> list  = criteria.list();
		
		pageModel.setList(list);
		System.out.println("商品分页数据"+list.get(0).toString());
		return pageModel;
	}
	/**
	 * 获得商品总条数
	 * @return
	 * @
	 */

	@Override
	public int getProductCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(id) from Product");
		List<Long> list = query.list();
		System.out.println("商品总条数"+list.get(0).intValue());
		return list.get(0).intValue();
	}
    /**
     * 添加商品
     */
	@Override
	public void addProduct(Product product) {
		
		sessionFactory.getCurrentSession().save(product);
	}
    /**
     * 根据id获得商品
     */
	@Override
	public Product getProductById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Product product = session.get(Product.class, id);
		return product;
	}
   /**
    * 更新商品
    */
	@Override
	public void updateProduct(Product product) {
		Session session = sessionFactory.getCurrentSession();
		session.update(product);

	}
    /**
     * 删除商品
     */
	@Override
	public void deleteProductById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Product product = getProductById(id);
		session.delete(product);

	}

	/**
	 * 查询所有商品
	 * @return
	 * @
	 */
	@Override
	public List<Product> getAllProducts() {
		List<Product> list = new ArrayList<Product>();
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Product");
		list=query.list();
		return list;
	}
	
	/**
	 * 获取最新产品
	 * @param num 返回的条数
	 * @return
	 * @
	 */
	@Override
	public List<Product> getNewProducts(int num) {
		Session session = sessionFactory.getCurrentSession();
		List<Product> list=new ArrayList<Product>();
		Query query=session.createQuery("from Product order by createTime desc");
		query.setMaxResults(num);
		list=query.list();
		
		return list;
	}
	
	/**
	 * 根据参数返回指定结果集
	 * @param product
	 * @param num
	 * @return
	 * @
	 */
	@Override
	public List<Product> getProductsByType(Product product, int num) {
		List<Product>  list = new ArrayList<Product>();
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createQuery("select p from Product p left join p.classification c"
				+ " where c.name =:name  ");  //select 可以知道返回那个对象  
		query.setMaxResults(num); 
		query.setParameter("name",product.getClassification().getName());
		list=query.list();
		return list;
	}
	
	/**
	 * 获取商品销量排行榜
	 * @return
	 * @
	 */
    @Override
	public List<Product> getProductRankings() {
    	Session session = sessionFactory.getCurrentSession();
		List<Product> list=new ArrayList<Product>();
		Query query=session.createQuery("from Product order by sales desc");
		query.setMaxResults(6); //LIMIT 在hibernate中没有  这里使用setMaxResults
		list=query.list();
		
		return list;
	}

}
